<?php
class Password extends CI_Controller {
	private $error = array();
	     
  	public function index() {	
    	if (!$this->customer->isLogged()) {
      		$this->session->set_userdata('redirect', site_url('account/password'));

      		$this->redirect(site_url('account/login'));
    	}

		$this->lang->load('account/password');

    	$this->document->setTitle($this->lang->line('heading_title'));
			  
    	if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
			$this->load->model('account/Model_Customer');
			
			$this->Model_Customer->editPassword($this->customer->getEmail(), $this->input->post('password'));
 
      		$this->session->set_userdata('success', $this->lang->line('text_success'));
	  
	  		redirect(site_url('account/account'));
    	}

      	$data['breadcrumbs'] = array();

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),       	
        	'separator' => false
      	); 

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_account'),
			'href'      => site_url('account/account'),
        	'separator' => $this->lang->line('text_separator')
      	);
		
      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('heading_title'),
			'href'      => site_url('account/password'),
        	'separator' => $this->lang->line('text_separator')
      	);
			
    	$data['heading_title'] = $this->lang->line('heading_title');

    	$data['text_password'] = $this->lang->line('text_password');

    	$data['entry_password'] = $this->lang->line('entry_password');
    	$data['entry_confirm'] = $this->lang->line('entry_confirm');

    	$data['button_continue'] = $this->lang->line('button_continue');
    	$data['button_back'] = $this->lang->line('button_back');
    	
		if (isset($this->error['password'])) { 
			$data['error_password'] = $this->error['password'];
		} else {
			$data['error_password'] = '';
		}

		if (isset($this->error['confirm'])) { 
			$data['error_confirm'] = $this->error['confirm'];
		} else {
			$data['error_confirm'] = '';
		}
	
    	$data['action'] = site_url('account/password');
		
		if ($this->input->post('password')) {
    		$data['password'] = $this->input->post('password');
		} else {
			$data['password'] = '';
		}

		if ($this->input->post('confirm')) {
    		$data['confirm'] = $this->input->post('confirm');
		} else {
			$data['confirm'] = '';
		}

    	$data['back'] = site_url('account/account');

		$data['header'] = helper_load_header();
		$data['column_left'] = helper_load_left();
		$data['column_right'] = helper_load_right();
		$data['content_top'] = helper_load_top();
		$data['content_bottom'] = helper_load_bottom();
		$data['footer'] = helper_load_footer();
		
		
		$this->load->view("account/password", $data);	
  	}
  
  	private function validate() {
    	if ((strlen(utf8_decode($this->input->post('password'))) < 4) || (strlen(utf8_decode($this->input->post('password'))) > 20)) {
      		$this->error['password'] = $this->lang->line('error_password');
    	}

    	if ($this->input->post('confirm') != $this->input->post('password')) {
      		$this->error['confirm'] = $this->lang->line('error_confirm');
    	}  
	
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}
}
?>
